/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guzik.lukasz.snpexplorer;

import SnpDbEntities.Category;
import guzik.lukasz.SnpDataAPI.SnpDataAPI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/**
 *
 * @author byku
 */
public class PublishingDialog implements ActionListener
{
    private JPublishingPanel jPublishingPanel;
    private DialogDescriptor dd;
    private int snpToPublishId;

    public PublishingDialog(JTree tree, int snpId)
    {
	snpToPublishId = snpId;
	jPublishingPanel = new JPublishingPanel();
	jPublishingPanel.jTree.setModel((DefaultTreeModel) tree.getModel());
	dd = new DialogDescriptor(jPublishingPanel, "Publikuj snippet",
		true, DialogDescriptor.OK_CANCEL_OPTION, null, this);
	dd.setClosingOptions(new Object[]
		{
		});
	dd.addPropertyChangeListener(new PropertyChangeListener()
	{
	    @Override
	    public void propertyChange(PropertyChangeEvent e)
	    {
		if(e.getPropertyName().equals(DialogDescriptor.PROP_VALUE)
			&& e.getNewValue() == DialogDescriptor.CLOSED_OPTION)
		{
		    dd.setClosingOptions(null);
		}
	    }
	});

	dd.setValid(false);

	TreeSelectionListener tsl = new TreeSelectionListener()
	{
	    @Override
	    public void valueChanged(TreeSelectionEvent e)
	    {
		DefaultMutableTreeNode selNode =
			(DefaultMutableTreeNode) jPublishingPanel.jTree.getLastSelectedPathComponent();

		if(selNode != null && (selNode.getUserObject() instanceof Category))
		{
		    dd.setValid(true);
		} else
		{
		    dd.setValid(false);
		}
	    }
	};

	jPublishingPanel.jTree.addTreeSelectionListener(tsl);
	jPublishingPanel.jTree.setCellRenderer(new ExtendedTreeCellRenderer());
    }

    @Override
    public void actionPerformed(ActionEvent event)
    {
	int publicCatId;
	DefaultMutableTreeNode selNode;

	if(event.getSource() == DialogDescriptor.CANCEL_OPTION)
	{
	    dd.setClosingOptions(null);
	} else
	{
	    selNode = (DefaultMutableTreeNode) jPublishingPanel.jTree.getLastSelectedPathComponent();
	    publicCatId = ((Category) selNode.getUserObject()).getId();
	    if(!SnpDataAPI.getDefault().addToPublishQueue(snpToPublishId, publicCatId))
	    {
		NotifyDescriptor nd = new NotifyDescriptor.Message(
			"Nie można wysłać snippetu do publikacji." +
			System.getProperty("line.separator") +
			"Być może snippet już czeka w kolejce na publikację",
			NotifyDescriptor.ERROR_MESSAGE);
		DialogDisplayer.getDefault().notify(nd);
	    } else
	    {
		dd.setClosingOptions(null);
	    }
	}
    }

    public void show()
    {
	DialogDisplayer.getDefault().notify(dd);
    }
}
